package org.bilinc.gz001.dao;


import org.apache.commons.lang.StringUtils;
import org.bilinc.gz001.Utils;
import org.bilinc.gz001.dao.interfaces.AbstractDaoImpl;
import org.bilinc.gz001.dao.interfaces.IMonthDao;
import org.bilinc.gz001.dao.recover.RecoverCalendar;
import org.bilinc.gz001.entity.Month;
import org.bilinc.gz001.entity.Resource;
import org.hibernate.NonUniqueResultException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

@Component
public class MonthDao extends AbstractDaoImpl<Month> implements IMonthDao {

    @Autowired
    ResourceDao resourceDao;

    @Autowired
    RecoverCalendar recoverCalendar;

    private final static String HQL_GET_MONTH = "SELECT r FROM Month r where r.yearNumber = :yearN AND r.monthNumber = :monthN AND r.resource.tabNumber=:tabelNomer";

    MonthDao() {
    }

    public void updateDataByDateAndResource(int year, int month, int day, String tabNumber, String newData) {
        Month monthWorkerData = (Month) getSessionStartTransaction().createQuery(HQL_GET_MONTH)
                .setInteger("yearN", year)
                .setInteger("monthN", month)
                .setString("tabelNomer", tabNumber)
                .uniqueResult();
        StringBuilder sb = new StringBuilder();
        String data = monthWorkerData.getData();
        String pattern = ";";


        int previousIndex = 0;
        int index = data.indexOf(pattern);
        int countDay = 1;


        for (int i= 0; i<Utils.DAYS_PER_MONTH[month]; i++){
            if (index < 0 || previousIndex < 0 ) {
                commitTransaction();
                System.out.println("Дней в месяце меньше чем нужно!");
                System.out.println("Должно быть " + Utils.DAYS_PER_MONTH[month] + " а их " + countDay);
                recoverCalendar.emergencyRecoverCalendarFromCorrupted(year, month, day, tabNumber);
                return;
            }
            if (countDay == day) {
                sb.append(newData + pattern);
            } else {
                sb.append(data.substring(previousIndex, index+1));
            }
            previousIndex = index + 1;
            index = data.indexOf(pattern, previousIndex);
            countDay++;
        }

        monthWorkerData.setData(sb.toString());
        commitTransaction();
    }

    public void addCalendarForResource(Resource resource) {
        Session session = getSessionStartTransaction();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i<= Calendar.DECEMBER; i++) {
            sb.setLength(0);
            for (int dayCount = 1; dayCount <= Utils.DAYS_PER_MONTH[i]; dayCount++) {
                sb.append(";");
            }
            Month monthWorkerData = new Month();
            monthWorkerData.setYearNumber(2013);
            monthWorkerData.setMonthNumber(i);
            monthWorkerData.setResource(resource);
            monthWorkerData.setData(sb.toString());
            session.save(monthWorkerData);
        }
        System.out.println("Добавили календарь для " + resource.getFullName());
        commitTransaction();
    }

}
